package com.pretty.test.controller.pets.user;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.pretty.test.controller.pets.user.dto.ChangePasswordOriginPasswordDTO;
import com.pretty.test.controller.pets.user.dto.FindPasswordAnswerDTO;
import com.pretty.test.controller.pets.user.dto.LoginDTO;
import com.pretty.test.controller.pets.user.vo.FindPasswordQuestionVO;
import com.pretty.test.controller.pets.user.vo.LoginVO;
import com.pretty.test.controller.pets.user.wrapper.LoginVOWrapper;
import com.pretty.test.entity.PetsStation;
import com.pretty.test.entity.PetsUser;
import com.pretty.test.service.IPetsUserService;
import com.pretty.test.utils.otherutils.Func;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 用户前端控制器
 * @author dujianyu
 * @since 2021-04-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/user")
public class PetsUserController {

    private IPetsUserService service;


    /**
     * 登录
     * @param dto
     */
    @PostMapping("/login")
    public R<LoginVO> submit(@RequestBody LoginDTO dto){
        if(!Func.isEmpty(dto.getId())){
            PetsUser user = new PetsUser();
            user.setId(dto.getId());
            user.setIsStation(dto.getIsStation());
            service.saveOrUpdate(user);
        }
        LoginVO vo = new LoginVO();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("username",dto.getUsername());
        PetsUser temp = service.getOne(queryWrapper);
        if(temp == null){
            vo.setIsLoginSuccess(0);
            return R.ok(vo);
        }else{
            if(!temp.getPassword().equals(dto.getPassword())){
                vo.setIsLoginSuccess(0);
                return R.ok(vo);
            } else {
              vo = LoginVOWrapper.build().entityVO(temp);
              vo.setIsLoginSuccess(1);
              return R.ok(vo);
            }
        }
    }

    /**
     * 注册
     * @param petsUser
     */
    @PostMapping("/register")
    public R<Boolean> submit(@RequestBody PetsUser petsUser){
        return R.ok(service.saveOrUpdate(petsUser));
    }

    /**
     * 用户名查重
     * @param username
     */
    @GetMapping("/checkUsername")
    public Boolean checkDuplicateUsername(String username){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",username);
        PetsUser result = service.getOne(queryWrapper);
        return result != null;
    }

    /**
     * 得到密保问题
     * @param username
     */
    @GetMapping("/getQuestionByUsername")
    public R<FindPasswordQuestionVO> getQuestionByUsername(String username){
        return R.ok(service.getQuestionByUsername(username));
    }

    /**
     * 检查密保问题答案
     * @param dto
     */
    @PostMapping("/checkUserAnswer")
    public R<String> checkUserAnswer(@RequestBody FindPasswordAnswerDTO dto){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("username",dto.getUsername());
        PetsUser data = service.getOne(queryWrapper);
        if(data.getQuestionOneAnswer().equals(dto.getQuestionOneAnswer()) && data.getQuestionTwoAnswer().equals(dto.getQuestionTwoAnswer())){
            return R.ok(data.getId().toString());
        }else{
            return R.ok("");
        }
    }

    /**
     * 检查原密码
     * @param dto
     */
    @PostMapping("/checkOriginPassword")
    public R<Boolean> checkOriginPassword(@RequestBody ChangePasswordOriginPasswordDTO dto){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_deleted",0);
        queryWrapper.eq("id",Func.toLongArray(dto.getId())[0]);
        PetsUser data = service.getOne(queryWrapper);
        if(data.getPassword().equals(dto.getOriginPassword())){
            return R.ok(true);
        }else{
            return R.ok(false);
        }
    }
}
